package com.cgnpc.scp.run.controller;

import cn.com.cgnpc.aep.bizcenter.appcenter.sdk.result.ApiResult;
import cn.com.cgnpc.aep.bizcenter.filecenter.api.vo.FileInfoVO;
import com.cgnpc.cud.annotation.OperatorType;
import com.cgnpc.cud.annotation.UBA;
import com.cgnpc.cud.core.common.util.MessageUtils;
import com.cgnpc.cud.core.domain.AjaxResult;
import com.cgnpc.framework.userinfo.CurrentUser;
import com.cgnpc.scp.common.bean.ScpApiResult;
import com.cgnpc.scp.common.bean.ScpFileInfoVO;
import com.cgnpc.scp.common.pojo.UserInfo;
import com.cgnpc.scp.common.service.FileCenterService;
import com.cgnpc.scp.common.utils.AjaxResultUtil;
import com.cgnpc.scp.common.utils.FileUtil;
import com.cgnpc.scp.config.MockClient;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileReader;
import java.util.*;

/**********************************
 * 用途说明: 调试经营管理 公共接口
 * 作者姓名: zhangyibing
 * 创建时间: 2020年10月16日11:37:00
 ***********************************/
@RestController
@RequestMapping("/scp/run/common")
public class RunCommonController {
    private static final Logger LOGGER = LoggerFactory.getLogger(MarketInfoController.class);

    @Autowired
    FileCenterService fileCenterService;

    @Autowired
    CurrentUser currentUser;

    @Autowired
    private MessageSource messageSource;

    @PostMapping("/uploadFile")
//    @UBA(module = "调试经营管理", action = "调试经验管理文件上传", channel = OperatorType.Button)
    @ApiOperation(value="调试经验管理文件上传")
    public AjaxResult uploadFile(HttpServletRequest request) {
        try {
            final String logInfo = "[run_file_uploadFile_UUID_" + UUID.randomUUID().toString() + "]";
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            List<MultipartFile> files = multipartRequest.getFiles("file");
            //System.out.println("-----"+files.size());

            // begin 校验文件类型
            boolean isSafeFileWithUploadFile = FileUtil.isSafeFileWithUploadFile(files);

            if (!isSafeFileWithUploadFile) {
                return AjaxResultUtil.createErrorMessage(
                        "上传文件类型不匹配"
                );
            }
            // end 校验文件类型

            // 获取当前的登录用户信息
            UserInfo loginUserInfo = currentUser.getCurrentUserDetail();
            // 获取请求的语言reqLocale，cookie需要包含lang=cn，或者lang=en
            Locale reqLocale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);

//            if("local".equals(devMode)) {
//                List list = new ArrayList();
//                list.add(request);
//                ApiResult fileUploadResult = mockClient.invoke("runCommonController", "uploadFile", list, ApiResult.class, MultipartFile.class);
//                System.out.println(fileUploadResult);
//                if(!"200".equals(fileUploadResult.getCode())){
//                    throw new Exception("runCommonController -- " + fileUploadResult.getMsg());
//                }
//                return AjaxResult.success().put("data", fileUploadResult);
//            }
            ScpApiResult<List<ScpFileInfoVO>> fileUploadResult = null;

                fileUploadResult = fileCenterService.uploadFileToFileCenterWithScpApiResult(logInfo, reqLocale,
                        loginUserInfo, files);
            System.out.println("fileUploadResult---"+ fileUploadResult);
            if("200".equals(fileUploadResult.getCode())){
                return AjaxResult.success().put("data", fileUploadResult);
            }else {
                return AjaxResult.error(fileUploadResult.getMsg()).put("data",fileUploadResult);
            }
        } catch (Exception e) {
            LOGGER.error("RunCommonController uploadFile error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

}
